package cn.edu.buaa.cnsatm.traffic_big_data_server.waterway.service;

import cn.edu.buaa.cnsatm.traffic_big_data_server.util.WebSpiderUtil;
import cn.edu.buaa.cnsatm.traffic_big_data_server.waterway.model.BoatModel;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
//import com.gargoylesoftware.htmlunit.BrowserVersion;
//import com.gargoylesoftware.htmlunit.WebClient;
//import com.gargoylesoftware.htmlunit.html.HtmlPage;
//import org.jsoup.nodes.Document;
//import org.openqa.selenium.*;
//import org.openqa.selenium.chrome.ChromeDriver;
//import org.openqa.selenium.support.ui.ExpectedConditions;
//import org.openqa.selenium.support.ui.Select;
//import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.*;
import java.math.BigInteger;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.StringTokenizer;
import java.util.concurrent.TimeUnit;

public class BoatService {
    public static void getBoatRecordFromWebAndSaveToDatabase(long beg, String cookie) throws InterruptedException, IOException {
        final String RECORD_URL_LEFT = "http://www.shipxy.com/ship/GetIHSData?mmsi=";
        final String POSITION_URL_LEFT = "http://www.shipxy.com/ship/GetIHSData?mmsi=";
        WebSpiderUtil.getJsonFromURL(RECORD_URL_LEFT, cookie);
        WebSpiderUtil.getJsonFromURL(POSITION_URL_LEFT);
        String s = "1000000000";

        //BigInteger minn = BigInteger.valueOf(Long.parseLong(s));
        File file = new File("boat.txt");
        FileOutputStream fos = null;
        if(!file.exists()){
            file.createNewFile();//如果文件不存在，就创建该文件
            fos = new FileOutputStream(file);//首次写入获取
        }else{
            //如果文件已存在，那么就在文件末尾追加写入
            fos = new FileOutputStream(file,true);//这里构造方法多了一个参数true,表示在文件末尾追加写入
        }

        OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");//指定以UTF-8格式写入文件

        for(long i = beg; i <= 999999999; i++){
            Thread.sleep(1000);
            osw.write("[" + i + "]" + "\n");
            System.out.println("mmsi=" + i);
            JSONObject jo = null;
            try{
                jo = JSON.parseObject(WebSpiderUtil.getJsonFromURL(RECORD_URL_LEFT+i, cookie));
            }catch (Exception e){
                BufferedReader br;
                br = new BufferedReader(new InputStreamReader(System.in));
                br.readLine();
            }


            try{
                JSONArray ja = jo.getJSONArray("data");
                BoatModel boat =(BoatModel)JSONObject.toJavaObject(ja.getJSONObject(0), BoatModel.class);
                osw.write(boat.toString() + "\n");
                osw.flush();
                System.out.println(boat.toString());
            }catch (Exception e){
                continue;
            }


        }
        osw.close();
        //BoatModel boat = new BoatModel();
//        boat.setIMO(ja.getJSONObject(0).getString("IMO")) ;
//        boat.setShip_name(ja.getJSONObject(0).getString("ShipName"));
//        boat.setMmsi(ja.getJSONObject(0).getString("MMSI"));
//        boat.setCallsign(ja.getJSONObject(0).getString("IMO"));

    }
    //
//    public static void getBoatInformationFromWebAndSaveToDatabase() throws InterruptedException, IOException, ParseException {
////        Document document = WebSpiderUtil.getHTMLDocument("http://ihs.shipxy.com/Ship/ShipListPage?&pindex=1");
////        System.out.println(document);
////        String s = document.getElementById("tableList").getElementsByTag("tbody").get(0).getElementsByTag("tr").get(0).getElementsByTag("th").get(0).text();
////
////        System.out.println(s);
//
//        // 根据form的名字获取页面表单，也可以通过索引来获取：page.getForms(
//
//        System.getProperties().setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
//
////开启新WebDriver进程
//
//        WebDriver webDriver = new ChromeDriver();
//
//        System.out.println("打开浏览器--------------------------------------------------------------------------------------------------------------");
//
//        webDriver.manage().window().maximize();
//
//        System.out.println("页面最大化--------------------------------------------------------------------------------------------------------------");
//
////全局隐式等待，等待
//
//        webDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//
////设定网址
//
//        webDriver.get("http://www.shipxy.com/");
//
////显示等待控制对象
//
//        WebDriverWait webDriverWait = new WebDriverWait(webDriver, 1);
//
////输入用户名 密码
//        WebElement ele = webDriver.findElement(By.xpath("//*[@id=\"leadPage\"]/div[2]/a"));
//        ele.click();
//        //webDriverWait=new WebDriverWait(webDriver,1);
//        Thread.sleep(6000);// 60秒
//        webDriver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);//等待界面加载完
//        ele = webDriver.findElement(By.xpath("//*[@id=\"leadPage\"]/div[3]/a"));
//        ele.click();
//        webDriver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
//        ele = webDriver.findElement(By.xpath("//*[@id=\"userCenter\"]/a[1]"));
//        //webDriver.
//        ele.click();
//        webDriver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
//        //webDriverWait=new WebDriverWait(webDriver,5);
//        ((JavascriptExecutor) webDriver).executeScript("ShipxyLogin.showKeyLogin()");
////        ele = webDriver.findElement(By.xpath("/html/body/div[11]/div/div[2]/p[4]/a[1]"));
////        ele.click();
//        webDriver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
//        webDriver.findElement(By.id("userName")).sendKeys("lxbuaa");
//        webDriver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
//        webDriver.findElement(By.id("userPWD")).sendKeys("lxbuaa");
//        //webDriverWait=new WebDriverWait(webDriver,1)
//        webDriver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
//        ((JavascriptExecutor) webDriver).executeScript("ShipxyLogin.checkForm()");
//        webDriver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
//        //webDriver.findElement(By.xpath("/html/body/div[1]/div[2]/span[1]/a[3]")).click();
//        File cookieFile = new File("csdn.cookie.txt");
//        cookieFile.delete();
//        cookieFile.createNewFile();
//        FileWriter fileWriter = new FileWriter(cookieFile);
//        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
//        for (Cookie cookie : webDriver.manage().getCookies()) {
//            bufferedWriter.write((cookie.getName() + ";" +
//                    cookie.getValue() + ";" +
//                    cookie.getDomain() + ";" +
//                    cookie.getPath() + ";" +
//                    cookie.getExpiry() + ";" +
//                    cookie.isSecure()));
//            bufferedWriter.newLine();
//        }
//        bufferedWriter.flush();
//        bufferedWriter.close();
//        BufferedReader bufferedReader;
//        FileReader fileReader = new FileReader(cookieFile);
//        bufferedReader = new BufferedReader(fileReader);
//        String line;
//        while ((line = bufferedReader.readLine()) != null) {
//            StringTokenizer stringTokenizer = new StringTokenizer(line, ";");
//            while (stringTokenizer.hasMoreTokens()) {
//
//                String name = stringTokenizer.nextToken();
//                String value = stringTokenizer.nextToken();
//                String domain = stringTokenizer.nextToken();
//                String path = stringTokenizer.nextToken();
//                Date expiry = null;
//                String dt;
//
//                if (!(dt = stringTokenizer.nextToken()).equals("null")) {
//                    dt = dt.substring(4);
//                    dt = dt.replace("Jan","01");
//                    dt = dt.replace("Feb","02");
//                    dt = dt.replace("Mar","03");
//                    dt = dt.replace("Apr","04");
//                    dt = dt.replace("May","05");
//                    dt = dt.replace("Jun","06");
//                    dt = dt.replace("Jul","07");
//                    dt = dt.replace("Aug","08");
//                    dt = dt.replace("Sep","09");
//                    dt = dt.replace("Oct","10");
//                    dt = dt.replace("Nov","11");
//                    dt = dt.replace("Dec","12");
//                    DateFormat sdf = new SimpleDateFormat("MM dd hh:mm:ss z yyyy");
//                    expiry = sdf.parse(dt);
//                }
//
//                boolean isSecure = new Boolean(stringTokenizer.nextToken()).booleanValue();
//                Cookie cookie = new Cookie(name, value, domain, path, expiry, isSecure);
//                webDriver.manage().addCookie(cookie);
//                break;
//            }
//            break;
//        }
//        webDriver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
//        Thread.sleep(3000);
//        //webDriver.get("http://ihs.shipxy.com/Ship/BSearchShipPage");
//        for(int i = 0; i <= 9999999; i++){
//            String num = String.valueOf(i);
//            for(int j = 0; j < 7 - num.length(); j++) num = "0" + num;
//            webDriver.get("http://ihs.shipxy.com/Ship/ShipDetailPage?sid=" + num);
//            Thread.sleep(500);
//            try{
//                ((JavascriptExecutor) webDriver).executeScript("javascript:exportPdf()");
//                Thread.sleep(500);
//            }catch (Exception e){
//                continue;
//            }
//        }
    //webDriver.get("http://ihs.shipxy.com/Ship/ShipDetailPage?sid=7822237");

//        if(!webDriver.findElement(By.xpath("//*[@id=\"tree1\"]/li[2]/ul/li[1]/input")).isSelected()) webDriver.findElement(By.xpath("//*[@id=\"tree1\"]/li[2]/ul/li[1]/input")).click();
//        if(!webDriver.findElement(By.xpath("//*[@id=\"tree1\"]/li[2]/ul/li[2]/input")).isSelected()) webDriver.findElement(By.xpath("//*[@id=\"tree1\"]/li[2]/ul/li[2]/input")).click();
//
//        WebElement table = webDriver.findElement(By.className("com_info"));
//        WebElement tbody = table.findElement(By.tagName("tbody"));
//        List<WebElement> tr = tbody.findElements(By.tagName("tr"));
//        System.out.println("基本信息");
//        for(WebElement td: tr){
//            System.out.println(td.getText());
//        }
//        System.out.println("吨位与尺寸");
//        List<WebElement> newTd = webDriver.findElements(By.className("tb_new_td_m"));
//        for(WebElement td: newTd){
//            System.out.println(td.getText());
//        }
    //
    //http://ihs.shipxy.com/Ship/ShipListPage?s=0.15723000596909786

    /*开爬*/
//        Select sel = new Select(webDriver.findElement(By.id("FlagCode")));
//        sel.selectByValue("CHR");
//        Thread.sleep(3000);
//        ((JavascriptExecutor) webDriver).executeScript("Search()");
//        Thread.sleep(3000);
//        //WebElement table = webDriver.findElement(By.id("tableList"));
//        int cnt = 0;
//        do{
//            List<WebElement> trList = webDriver.findElements(By.tagName("tr"));
//            for(WebElement tr: trList){
//                System.out.println(trList.size());
//                List<WebElement> thList = webDriver.findElements(By.tagName("td"));
//                for(WebElement th: thList){
//
//                    System.out.println(th.getText());
//                }
//            }
//            //Thread.sleep(3000);
//            webDriver.findElement(By.className("next")).click();
//            Thread.sleep(3000);
//            cnt += 1;
//        }while(cnt < 3);

    //webDriver.quit();
//                sel.selectByValue("CHT");
    //}
    public static void main(String args[]) throws InterruptedException, IOException, ParseException {

        getBoatRecordFromWebAndSaveToDatabase(
                273310731,"_elane_maptype=MT_SEA; Hm_lvt_adc1d4b64be85a31d37dd5e88526cc47=1571897877,1571918706,1573730663; tc_TC=; _elane_shipfilter_type=%u8D27%u8239%2C%u96C6%u88C5%u7BB1%u8239%2C%u6CB9%u8F6E%2C%u5F15%u822A%u8239%2C%u62D6%u8F6E%2C%u62D6%u5F15%2C%u6E14%u8239%2C%u6355%u635E%2C%u5BA2%u8239%2C%u641C%u6551%u8239%2C%u6E2F%u53E3%u4F9B%u5E94%u8239%2C%u88C5%u6709%u9632%u6C61%u88C5%u7F6E%u548C%u8BBE%u5907%u7684%u8239%u8236%2C%u6267%u6CD5%u8247%2C%u5907%u7528-%u7528%u4E8E%u5F53%u5730%u8239%u8236%u7684%u4EFB%u52A1%u5206%u914D%2C%u5907%u7528-%u7528%u4E8E%u5F53%u5730%u8239%u8236%u7684%u4EFB%u52A1%u5206%u914D%2C%u533B%u7597%u8239%2C%u7B26%u540818%u53F7%u51B3%u8BAE%28Mob-83%29%u7684%u8239%u8236%2C%u62D6%u5F15%u5E76%u4E14%u8239%u957F%3E200m%u6216%u8239%u5BBD%3E25m%2C%u758F%u6D5A%u6216%u6C34%u4E0B%u4F5C%u4E1A%2C%u6F5C%u6C34%u4F5C%u4E1A%2C%u53C2%u4E0E%u519B%u4E8B%u884C%u52A8%2C%u5E06%u8239%u822A%u884C%2C%u5A31%u4E50%u8239%2C%u5730%u6548%u5E94%u8239%2C%u9AD8%u901F%u8239%2C%u5176%u4ED6%u7C7B%u578B%u7684%u8239%u8236%2C%u5176%u4ED6; _elane_shipfilter_length=0%2C40%2C41%2C80%2C81%2C120%2C121%2C160%2C161%2C240%2C241%2C320%2C321%2C9999; _elane_shipfilter_sog=0%2C1; _elane_shipfilter_country=1; _filter_flag=-1; _elane_shipfilter_olength=0; tc_QX=; leadPageCookie=1; shipxy_v3_history_serch=s%u2606YANCHENGJI01805%u2606413900011%u260670%u2606MMSI%uFF1A413900011%7Cs%u2606JUFENG829%u2606413903389%u260670%u2606MMSI%uFF1A413903389%7Cp%u2606%u5317%u6D25%u2606213527%u260621.833334%u2606111.966667%7Cp%u2606Gangavaram%u2606218139%u260617.62645%u260683.2484%7Cs%u2606AO%20TOU%203243%u26063243%u2606%u2606MMSI%uFF1A3243%7Cs%u2606AO%20TOU%206789%2002%u2606678902%u2606%u2606MMSI%uFF1A678902%7Cs%u2606TRAILBLAZER%u2606366082000%u260630%u2606IMO%uFF1A7729069%7Cs%u2606SYMI%20I%u2606636016306%u2606100%u2606MMSI%uFF1A636016306%7Cs%u2606DONGPING120651%u260650087102%u2606%u2606MMSI%uFF1A50087102%7Cs%u2606YUEDONGGUANYU92020%u2606412468699%u260630%u2606MMSI%uFF1A412468699; FD857C2AF68165D4=/vvpckUpWYYPrD3xHgBRIRfAFFZ7g5Qtd/NwsgnBrWv25B0asvXyNO2Ia9rk7IUZvEBqLOgJBzM=; SERVERID=ce54c768aca7be22386d8a7ce24ecdae|1574074639|1574074622; .UserAuth2=DEF70643D3242B2501A3AAC7F01C5A845EC991851684F24D7A1D0001CFE24B6F065A77C4250EEF213A6D587662924088CEB32719FBE229B8ECDA6C3B8C6461E5858E2680DBFF8B6DF785B63764C78EA05B2EC122BF89FEECFB6AB3DD557B6A4C84A66163E545E0DA8183787AC70C91F59F9B6546"
        );
    }
}
